// pages/component/carddepots/getcardrecord/getcardrecord.js
var GlobalData = require('../../../../utils/GlobalData.js');
var dataManager = require('../../../../utils/dataManager.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordArr: [],
    page: 20,
    arrlength: 0,
    haveData: false,
    hidden: true,
    isUsed:false,
    isUsedarr: [{ src: "/images/quanbu.png", text: "查看可用" }, { src: "/images/keyong.png", text: "查看全部" }],
    isUsedsrc:{ src: "/images/quanbu.png", text: "查看可用" }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    dataManager.removeBtnObserver("getcardrecord")
    this.setData({
      hidden: true
    })
    GlobalData.isjiLu = false;
    if (GlobalData.GetCardRecord) {
      this.onComplete();
    } else {
      dataManager.rigisterObserver("getcardrecord", this.onComplete, this);
      dataManager.getGetCardRecord(GlobalData.GetCardRecordother["index"], GlobalData.GetCardRecordother["size"], GlobalData.GetCardRecordother["add"]);
    }
  },
  onComplete: function () {
    var temp = GlobalData.clone(GlobalData.GetCardRecord);
    if (GlobalData.GetCardRecord.length == this.data.arrlength)
      return;
    // if (GlobalData.GetCardRecord)
    //  temp 
    for (let i = 0; i < temp.length; i++) {
      let ct = temp[i].createtime.split('T');
      temp[i]["date"] = ct[0];
      temp[i]["time"] = ct[1];
      if (temp[i]["isused"] == 0) {
        temp[i]["isUse"] = "未使用";
      } else {
        temp[i]["isUse"] = "已使用";
      }
    }
    this.setData({
      recordArr: temp,
      arrlength: temp.length
    })
    // this.data.arrlength = temp.length;
    /**判断是否有数据 */
    if (temp.length)
      this.setData({
        haveData: true
      })
  },
  /**查看可用*/
  bindIsuse:function(){
    let arr = GlobalData.clone(GlobalData.GetCardRecord);
    this.data.isUsed = !this.data.isUsed;
    if (this.data.isUsed){
      this.setData({
        isUsedsrc: this.data.isUsedarr[1]
      })
      arr = this.data.recordArr.filter(function (item) {
        if (item["isused"] == 0) {
          return item;
        }
      })
  
    }else{
      this.setData({
        isUsedsrc: this.data.isUsedarr[0]
      })
    }
    for (let i = 0; i < arr.length; i++) {
      let ct = arr[i].createtime.split('T');
      arr[i]["date"] = ct[0];
      arr[i]["time"] = ct[1];
      if (arr[i]["isused"] == 0) {
        arr[i]["isUse"] = "未使用";
      } else {
        arr[i]["isUse"] = "已使用";
      }
    }
    this.setData({
      recordArr: arr
    })
  },
  /**前往购卡详情 */
  goGetCardDetail: function (e) {
    var card = e.currentTarget.dataset.id;
    GlobalData.carddetail = card.id;
    this.setData({
      hidden: false
    })
    dataManager.OnRequestPost({ method: "getagentcdkeylog", "token": GlobalData.token, "id": card.id }, this.onProcessGetCardDetail, false, this.onError);
  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  onProcessGetCardDetail: function (data) {
    GlobalData.GetCardDetail = data;

    // if (dataManager.isClick("getcarddetail")) {
    //   return
    // } else {
    //   dataManager.btnObserver("getcarddetail")
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../carddepots/getcarddetail/getcarddetail', false);
    // }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    GlobalData.GetCardRecordother["index"] = GlobalData.GetCardRecordother["index"] + 1;
    dataManager.rigisterObserver("getcardrecord", this.onComplete, this);
    dataManager.getGetCardRecord(GlobalData.GetCardRecordother["index"], GlobalData.GetCardRecordother["size"], true);
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})